﻿using System;
using System.Linq;
using System.Windows.Forms;
using Liang.UI;
using WeifenLuo.WinFormsUI.Docking;
using Liang.DataBase.BLL;
using Liang.UI.Tools;
using Liang.DataBase.Model.Entity;
using Liang.DataBase.Tools;
using Liang.DataBase.Model;
using Liang.Model;

namespace Client
{
    public partial class FrmBasicdata : DockContent
    {
        BasicdataBLL basicdataBLL = new BasicdataBLL();

        public FrmBasicdata()
        {
            InitializeComponent();
        }

        private void FrmBasicdata_Load(object sender, EventArgs e)
        {
            initView();
        }

        /// <summary>
        /// 初始化gridView
        /// </summary>
        public void initView()
        {
            dataView.AutoGenerateColumns = false;
            dataView.Columns.Add(FormHelper.createTextColumn("id", "basicdataId", 100, false));
            dataView.Columns.Add(FormHelper.createTextColumn("代码", "code", 100, true));
            dataView.Columns.Add(FormHelper.createTextColumn("名称", "des", 200, true));
            //追加
            dataView.Columns.Insert(dataView.Columns.Count, FormHelper.createLinkColumn("Detail", "编辑", true));
            this.pager.Bind();//绑定  
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private int queryBind()
        {
            // 分页信息
            PageInfo pagerInfo = new PageInfo()
            {
                CurrentPageIndex = pager.PageCurrent,
                PageSize = pager.PageSize,
                RecordCount = pager.PageSize * pager.PageCurrent
            };

            // 查询条件
            var where = LambdaHelper.True<sys_basicdata>();

            if (!string.IsNullOrEmpty(this.extName.Text.Trim()))
            {
                where = where.And(p => this.extName.Text.Trim().Contains(p.des));
            }

            var list = basicdataBLL.FindWithPagerAndOrderby(where, ref pagerInfo, "basicdataId");

            //绑定分页控件   
            pager.bindingSource.DataSource = list;
            pager.bindingNavigator.BindingSource = pager.bindingSource;
            //分页控件绑定DataGridView   
            dataView.DataSource = pager.bindingSource;
            //返回总记录数   
            return pagerInfo.RecordCount;
        }

        /// <summary>
        /// 显示编辑页面
        /// </summary>
        public void showEdit(sys_basicdata basicdata = null)
        {
            FrmBasicdataEdit form = new FrmBasicdataEdit(basicdata);

            if (DialogResult.OK == form.ShowDialog())
            {
                pager.Bind();
            }
        }

        private void dataView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataView.Columns[e.ColumnIndex].Name == "Detail" && 0 < e.RowIndex)
            {
                Guid basicdataId = (Guid)dataView.Rows[e.RowIndex].Cells["basicdataId"].Value;

                sys_basicdata basicdata = basicdataBLL.FindByID(basicdataId);

                if (null == basicdataId)
                {
                    MessageBox.Show("未知的错误");
                }
                else
                {
                    showEdit(basicdata);
                }
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            pager.Bind();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            showEdit();
        }

        private int pager_EventPaging()
        {
            return queryBind();
        }

        private EventPagingArg pager_EventDelItem()
        {
            return new EventPagingArg("类型定义删除", this.dataView);
        }

        private EventPagingArg pager_EventExcel()
        {
            return new EventPagingArg("类型定义管理", this.dataView);
        }
    }
}
